//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
var Isshow = false;
var Isshow1 = false;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 
    imgUrl: baseJs.imgUrl,
    // 
    topId: 0,
    leftId: 2,
    // 左侧数据
    leftData: [],
    // 右侧数据
    rightData: [],
    TypePic: "",
    // 顶部
    topData: [{
      value: "易商品",
      id: 0,
      leftId: 2,
      active: true
    },
    {
      value: "易服务",
      id: 1,
      leftId: 27,
      active: false
    },

    ],
    // 
    uid: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var show = wx.getStorageSync('showStatus1');
    // if (show != 1) {
    //   if (show == 0) {
    //     that.setData({
    //       Isshow: true,
    //       Isshow1: false,
    //       IsshowUrlString:wx.getStorageSync('showStatustest3'),
    //     })
    //   } if (show == 1) {
    //     that.setData({
    //       Isshow1: true,
    //       Isshow: false,
    //     })
    //   }
    //   return;
    // }else{
    //   that.setData({
    //     Isshow1: true,
    //     Isshow: false,
    //   })
    // }
    // 更改：
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetIsServers"),
      success: function (res) {
        console.log(JSON.parse(baseJs.decryUrl(res.data)));
        var ResultData = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        // wx.setStorageSync('showStatus1', ResultData.Status);
        // wx.setStorageSync('showStatustest1', ResultData.test1);
        // wx.setStorageSync('showStatustest2', ResultData.test2);
        // wx.setStorageSync('showStatustest3', ResultData.test3);
        // wx.setStorageSync('showStatustest4', ResultData.test4);
        if (ResultData.Status == 0) {
          that.setData({
            Isshow: true,
            Isshow1: false,
            IsshowUrlString: ResultData.test3,
          })
          // console.log(IsshowUrlString);
        } else {
          that.setData({
            Isshow: false,
            Isshow1: true,
          })
          that.setData({
            uid: wx.getStorageSync('uid'),
          })
          that.GetGoodsType();
          that.GetGoodsTypeSecond();
        }
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 顶部点击
  topTap: function (e) {
    var that = this;
    // console.log(e);
    var topData = this.data.topData;
    var leftData = this.data.leftData;
    var rightData = this.data.rightData;
    var value = e.currentTarget.dataset.value;
    var topId = e.currentTarget.dataset.id;
    var leftId = e.currentTarget.dataset.leftid;
    for (var i = 0, len = topData.length; i < len; ++i) {
      if (topData[i].value == value) {
        topData[i].active = true;
      } else {
        topData[i].active = false
      }
    }
    this.setData({
      topData: topData,
      value: value,
      topId: topId,
      leftId: leftId,
    })
    leftData = [];
    rightData = [];
    this.GetGoodsType();
    this.GetGoodsTypeSecond();
  },
  // 左侧点击
  leftTap: function (e) {
    var that = this;
    // console.log(e);
    var leftData = this.data.leftData;
    var rightData = this.data.rightData;
    var value = e.currentTarget.dataset.value;
    var leftId = e.currentTarget.dataset.id;
    for (var i = 0, len = leftData.length; i < len; ++i) {
      if (leftData[i].TypeName == value) {
        leftData[i].active = true;
      } else {
        leftData[i].active = false
      }
    }
    this.setData({
      leftData: leftData,
      value: value,
      leftId: leftId
    })
    rightData = [];
    this.GetGoodsTypeSecond();
  },
  // 易商品、易服务
  GetGoodsType: function () {
    var that = this;
    var topId = this.data.topId;
    var leftData = this.data.leftData;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetGoodsType&GoodsOrService=" + topId),
      success: function (res) {
        leftData = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        for (var i = 0; i < leftData.length; i++) {
          leftData[i].active = false;
          leftData[0].active = true;
        }
        that.setData({
          leftData: leftData,
        })
        console.log(leftData);
      }
    })
  },
  //左侧点击
  GetGoodsTypeSecond: function () {
    var that = this;
    var leftId = this.data.leftId;
    var rightData = this.data.rightData;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetGoodsTypeSecond&OneId=" + leftId),
      success: function (res) {
        rightData = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        var arr = [];
        for (var key in rightData) {
          var obj = {
            name: key,
            list: rightData[key],
          };
          arr.push(obj);
        }
        rightData = arr;
        that.setData({
          rightData: rightData,
        })
        console.log(rightData);
      }
    })
  }

})